package hu.uszeged.inf.stud.prf.todo.bean.resource;

import hu.uszeged.inf.stud.prf.todo.dao.resource.ResourceDAO;
import hu.uszeged.inf.stud.prf.todo.entity.resource.Resource;
import hu.uszeged.inf.stud.prf.todo.entity.user.User;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

@ManagedBean
@ViewScoped
public class ResourceBean {
	
	@Inject
	private ResourceDAO resourceDAO;
	
	private Long id;
	
	private Resource res;
	
	public void load() {
		if (null == id) {
			res = new Resource();
		} else {
			res = resourceDAO.findById(id, false);
		}
	}
	
	public String save(User user) {
		res.setUser(user);
		resourceDAO.makePersistent(res);
		resourceDAO.flush();
		return "/content/resource/list?faces-redirect=true";
	}
	
	public String remove() {
		res.setArchive(true);
		resourceDAO.makePersistent(res);
		resourceDAO.flush();
		return "/content/resource/list?faces-redirect=true";
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public Resource getRes() {
		return res;
	}
	
	public void setRes(Resource res) {
		this.res = res;
	}

}
